package org.mashart.pink.m.gui;

import com.sun.lwuit.*;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;

/**
 *
 * @author peng
 */
public class Compass extends Component{
    /**
     * The heading course over ground. The unit is degree. True north is 0 degree.
     */
    private int course;
    /**
     * Angle from starting waypoint
     */
    private int directionFrom;
    /**
     * The text to display
     */
    private String [] text;
    /**
     * Side length of the compass, which is limited by the Width/Height of this component.
     */
    private int len;
    /**
     * Line Color
     */
    private static final int color = 0x00ff00;
    /**
     * Preferred Size
     */
    private static final int DEFAULT_LEN = 200;

    /**
     * Constructor.
     */
    Compass(){
        super();
        // not focusable
        setFocusable(false);

        int width = getWidth();
        int height = getHeight();
        // side length of largest square within the height/width limitation
        len = Math.min(height, width);
        // if no constraint on dimension
        if ( len == 0 ) {
            len = DEFAULT_LEN;
        }
    }

    /**
     * Set the course.
     * @param crs the heading course in degree. True north is 0 degree.
     */
    public void setCourse ( int c) {
        course = c;
    }

    /**
     * Set the string to be written
     * @param d string of distance
     */
    public void setText(String [] ts) {
        text = ts;
        repaint();
    }

    /**
     * Set the direction angle of the starting waypoint
     */
    public void setDirectionFrom ( int d ) {
        directionFrom = d;
    }

    /**
     * Paint the
     * @param g the component graphics
     */
    public void paint ( Graphics g ) {
        // get the font
        Style style = getStyle();
        Font fnt = style.getFont();
        int padding = style.getPadding(Component.LEFT);

        // get the coordinate of the component
        int X = getX() + padding;
        int Y = getY() + padding;
        // the length of the arrow decorator
        int al = len / 8;

        // calculate two endpoint of the arrow
        int r = len / 2 - padding;
        // course angle in radians
        double seta = Math.toRadians( 360 - course);
        // one endpoint
        int xn = (int) ( X + r + r * Math.sin( seta ) );
        int yn = (int) ( Y + r - r * Math.cos( seta ) );
        // another endpoint
        int xs = (int) ( X + r - r * Math.sin( seta ) );
        int ys = (int) ( Y + r + r * Math.cos( seta ) );
        // left endpoint of the arrow decorator
        double seta_l = seta + Math.PI / 4 ;
        int xl = (int) ( xn - al * Math.sin(seta_l) );
        int yl = (int) ( yn + al * Math.cos(seta_l) );
        // right endpoint of the arrow decorator
        double seta_r = Math.PI / 4 - seta;
        int xr = (int) ( xn + al * Math.sin(seta_r) );
        int yr = (int) ( yn + al * Math.cos(seta_r) );
        // west point
        int xw = (int) ( X + r - r * Math.cos( seta ) );
        int yw = (int) ( Y + r - r * Math.sin( seta ) );
        // east point
        int xe = (int) ( X + r + r * Math.cos( seta ) );
        int ye = (int) ( Y + r + r * Math.sin( seta ) );

        // use black color
        g.setColor(0x000000);
        // draw a circle
        g.drawArc(X, Y, len - padding * 2, len - padding * 2, 0, 360);
        
        // use green color
        g.setColor(0x00ff00);
        // draw east-west
        g.drawLine(xe, ye, xw, yw);

        // use blue color
        g.setColor(0x0000ff);
        // draw the center-south
        g.drawLine(xs, ys, X + r, Y + r);

        // use red color
        g.setColor(0xff0000);
        // draw the center-north
        g.drawLine(xn, yn, X + r, Y + r);
        g.drawLine(xn, yn, xl, yl);
        g.drawLine(xn, yn, xr, yr);

        

        // draw the letter 'N' at the head of the arrow
//        g.drawChar( 'N', (x4 - r) * 9 / 10 + r, (y4 - r) * 9 /10 + r );
        g.drawChar( 'N', xr, yr );


        // height of a line
        int h = fnt.getHeight();

        if ( text != null ) {
            // number of the lines
            int n = text.length;
            int x;
            // y-coordinate of current string
            int y = (int) ( Y + r - n * h / 2 ) ;
            // use black color
            g.setColor(0x000000);
            for ( int i = 0 ; i < n ; i++ ) {
                if ( text[i] != null ) {
                    // move left by half of the string width, i.e, align to center
                    x = X + r - fnt.stringWidth(text[i]) / 2 ;
                    g.drawString(text[i], x, y);
                }
                // move down a line
                y += h;
            }
        }
        

        // use red color
        g.setColor(0xff0000);
        // gap between arc of direction and the circle of compass
        int gap = 5;
        // draw the arc representing the direction angle of starting waypoint
        g.drawArc(X + gap, Y + gap, len - padding * 2 - gap * 2, len - padding * 2 - gap * 2, directionFrom + 180 + course, 15);
    }
    
    /**
     * Calculates the preferred size based on component content.
     * @return the calculated preferred size based on component content
     */
    protected Dimension calcPreferredSize() {
//        Display display = Display.getInstance();
//        int w = display.getDisplayWidth();
//        int h = display.getDisplayHeight();
//
//        // get the font
//        Style style = getStyle();
//        Font fnt = style.getFont();
//        // height of text (Label)
//        int textHeight = fnt.getHeight();
//        // height of two lines of text (two rows of Label)
//        int height = h - textHeight * 2 - style.getPadding(Component.TOP) - style.getPadding(Component.BOTTOM);
//        int width = w;

        // get the font
        Style style = getStyle();
        return new Dimension(
                len,
                len);
    }
}
